package cn.kgc.crowdfundingadminspringboot.controller;

import cn.kgc.crowdfundingadminspringboot.common.ResultEntity;
import cn.kgc.crowdfundingadminspringboot.pojo.Message;
import cn.kgc.crowdfundingadminspringboot.service.MessageService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/message")
public class MessageController {
    @Resource
    MessageService messageService;

    @RequestMapping("/to/page.html")
    public String toPage(){
        return "message-page";
    }


    @ResponseBody
    @RequestMapping("/search/by/keyword")
    public ResultEntity<PageInfo<Message>> search(
            @RequestParam(value="pageNum", defaultValue="1",required = false) Integer pageNum,
            @RequestParam(value="pageSize", defaultValue="5",required = false) Integer pageSize,
            @RequestParam(value="keyword", defaultValue="",required = false) String keyword
    ) {
        // 1.查询得到PageInfo对象
        PageInfo<Message> pageInfo = messageService.queryForKeywordWithPage(pageNum, pageSize, keyword);

        // 2.封装结果对象返回
        //返回的数据，成功并且包含数据
        return ResultEntity.successWithoutData(pageInfo);
    }

    @ResponseBody
    @RequestMapping("/get/list/by/id/list")
    public ResultEntity<List<Message>> getRoleListByIdList(@RequestBody List<Integer> roleIdList) {
        List<Message> roleList = messageService.getTagListByIdList(roleIdList);
        return ResultEntity.successWithoutData(roleList);
    }


    @ResponseBody
    @RequestMapping("/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> roleIdList) {
        messageService.batchRemove(roleIdList);
        return ResultEntity.successWithoutData();
    }


    @ResponseBody
    @RequestMapping("/save/role")
    public ResultEntity<String> saveRole(@RequestParam("memberid")Integer memberid,
                                         @RequestParam("content")String content) {
        System.out.println(content);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        messageService.saveRole(memberid, content, simpleDateFormat.format(new Date()));
        return ResultEntity.successWithoutData();
    }


    @ResponseBody
    @RequestMapping("/update/role")
    public ResultEntity<String> updateRole(Message role) {
        System.out.println(role);
        messageService.updateRole(role);
        return ResultEntity.successWithoutData();
    }

}
